///////////////////////////////////////////////////////////////////////////////
//	FILENAME:	Bounce.m
//	SUMMARY:	Implementation of Bouncy annotations to eText documents
//	SUPERCLASS:	Object
//	INTERFACE:	None
//	PROTOCOLS:	<DocNotification, Annotation, Tool>
//	AUTHOR:		Rohit Khare
//	COPYRIGHT:	(c) 1994 California Institure of Technology, eText Project
///////////////////////////////////////////////////////////////////////////////
//	DESCRIPTION
//		Glue for Ali Ozer's LinesView code. Demo of <Annotation>
///////////////////////////////////////////////////////////////////////////////
//	HISTORY
//	10/31/94:	Converted to eText5.0 inspector protocol.
//	02/04/94:	Created. Derived from NextDeveloper/Examples/Appkit/Lines.
///////////////////////////////////////////////////////////////////////////////

#import "Bounce.h"

@implementation Bounce
+ toolAwake:theApp
{
	char        buf[MAXPATHLEN];
	NXBundle	*bundle;
	NXImage		*theIcon;

	bundle = [NXBundle bundleForClass:[Bounce class]];
	if ( [bundle getPath:buf forResource:"BounceIcon" ofType:"tiff"] ) {
		theIcon = [[NXImage alloc] initFromFile:buf];
	} else {
		NXLogError("Image not found: BounceIcon");
		theIcon = [NXImage findImageNamed:"NXdefaulticon"];
	}
	[theApp   registerAnnotation: [Bounce class] 
							name: "Bounce"
					RTFDirective: "Bounce"
					   menuLabel: "Fun Stuff/Insert Bouncy..."
						 menuKey: '\0'
						menuIcon: (NXImage *) theIcon];
	return self;
}

-init 
{
	char        buf[MAXPATHLEN];
	NXBundle   *bundle;
	NXRect		frame;

	[super init];
	bundle = [NXBundle bundleForClass:[Bounce class]];
	if ( [bundle getPath:buf forResource:"Bounce" ofType:"nib"] ) {
		[NXApp loadNibFile:buf owner:self withNames:NO];
	} else {
		NXLogError("NIB not found: Bounce");
	}
	controlView = [controlPanel contentView];
	frame.origin.x = frame.origin.y = 0.0;
	frame.size.width = frame.size.height = 48.0;
	sz.width = sz.height = 48.0;
	corners = 5;
	linesView = [[LinesView alloc] initFrame:&frame];
	[linesView setNumberOfCorners:self];
	return self;
}
- initFromPboard:thePboard inDoc:theDoc linked:(BOOL) linked
{
	[self init];
	return self;
}

- free {
	[[NXApp inspector] invalidate];
	etDoc=nil;
	[linesView off];
	[linesView removeFromSuperview];
	[NXApp delayedFree:linesView];
	[controlPanel free];
	return [super free];
}

- (int)intValue {return corners;}

- readRichText:(NXStream *)stream forView:view 
{
	NXScanf(stream, "%f %f %d", &sz.height, &sz.width, &corners);
	[linesView setNumberOfCorners:self];
	return self;
}

- writeRichText:(NXStream *)stream forView:view
{
	NXPrintf(stream, "%f %f %d", sz.height, sz.width, corners);
	return self;
}

- calcCellSize:(NXSize *)theSize
{
	theSize->width = sz.width;
	theSize->height = sz.height;
	return self;
}
- highlight:(const NXRect *)cellFrame inView:controlView lit:(BOOL)flag
{
  	static BOOL highlighted=NO;
    if (highlighted != flag) {
		highlighted = flag;
		
		/* toggle highlighting */
			NXHighlightRect(cellFrame);
		
		/* make change visible */
		//[[cView window] flushWindow];
    }
    
    return self;
}
- drawSelf:(const NXRect *)cellFrame inView:cView
{
	
    PSgsave();
	if ([linesView superview] == NULL)
		[cView addSubview:linesView];
	// To show a view, we do a move/display combination
	[linesView moveTo:cellFrame->origin.x :cellFrame->origin.y];
	[linesView sizeTo:sz.width :sz.height];
    PSgrestore();
	[linesView display];
    return self;
}

- (BOOL) 	trackMouse:(NXEvent *)event
			inRect:(const NXRect *)cellFrame
			ofView:cView
{
	NXPoint mouseLocation;
	
    mouseLocation = event->location;
    [cView convertPoint:&mouseLocation fromView:NULL];

    if (NXPointInRect(&mouseLocation, cellFrame))
		if (event->data.mouse.click == 2)
			[self doubleClick:self];
		else if (event->data.mouse.click == 1)
 		{
			[[NXApp inspector] inspect: self];
			if (!(event->flags && NX_COMMANDMASK))
				[self click:self];
		}
	return YES;
}

- click:sender
{
	sz.width = [widthField floatValue];
	sz.height = [heightField floatValue];
	corners = [cornerField intValue];
	[linesView setNumberOfCorners:self];
	[linesView toggleRun:sender];
	return self;
}
- doubleClick:sender
{
	return self;
}


- editCorners:sender
{
	corners = [cornerField intValue];
	return [linesView setNumberOfCorners:self];
}

- editSize:sender
{
	sz.width = [widthField floatValue];
	sz.height = [heightField floatValue];
	return self;
}



- (const NXAtom *) types
{
	static NXAtom types[2] = {NULL, NULL};
	
	if (!types[0]) {
		types[0] = NXUniqueString("Controls");
	}
	return types;
}

- (const char *) inspectorTitle
{
	return  NXUniqueString("Bouncing Lines");
}

- resignInspector: (View *) oldInspector ofType: (const char *) type 
{
	sz.width = [widthField floatValue];
	sz.height = [heightField floatValue];
	corners = [cornerField intValue];
	[linesView setNumberOfCorners:self];
	return self;
}

- activateInspector: (View *) newInspector ofType: (const char *) type 
{
	[widthField setFloatValue:sz.width];
	[heightField setFloatValue:sz.height];
	[cornerField setIntValue:corners];
	return self;
}

- inspectorForType:(const char *) type 
{
	if(!strcmp(type,NXUniqueString("Controls")))
		return controlView;
	else NXLogError("Massive Inspector Failure: Asked Bouncy for: %s", type);
	return controlView;
}

@end